package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Examen;


public class ExamenDAO implements IExamenDAO {
    private static final Log log = LogFactory.getLog(ExamenDAO.class);

    // property constants
    //public static final Integer  EXAGRADO = "exagrado";
    public static final String EXAGRADO = "exagrado";

    //public static final String  EXAMDESCRIPT = "examdescript";
    public static final String EXAMDESCRIPT = "examdescript";

    //public static final Integer  EXAMESTADON = "examestadon";
    public static final String EXAMESTADON = "examestadon";

    //public static final Integer  EXAMIDN = "examidn";
    public static final String EXAMIDN = "examidn";

    //public static final String  EXAMNOMBREV = "examnombrev";
    public static final String EXAMNOMBREV = "examnombrev";

    //public static final Integer  EXAMORDENN = "examordenn";
    public static final String EXAMORDENN = "examordenn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Examen Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Examen instance) {
        log.debug("saving Examen instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Examen Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Examen instance) {
        log.debug("deleting Examen instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Examen Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Examen instance) {
        log.debug("updating Examen instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Examen findById(Integer id) {
        log.debug("finding Examen instance with id: " + id);

        try {
            Examen instance = (Examen) getSession().get(Examen.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Examen failed", re);
            throw re;
        }
    }
    
    
	public List<Examen> findByExa_examidn(Integer exa_examidn) {
		log.debug("finding all Calificacion instances");
        try {
            String queryString = "from Examen where exa_examidn ="+exa_examidn;
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
	
	

    public List<Examen> findByExample(Examen instance) {
        log.debug("finding Examen instance by example");

        try {
            List results = getSession().createCriteria("Examen")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Examen entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Examen> found by query
        */
    public List<Examen> findByProperty(String propertyName, Object value) {
        log.debug("finding Examen instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Examen as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Examen> findByExagrado(Object exagrado) {
        return findByProperty(EXAGRADO, exagrado);
    }

    public List<Examen> findByExamdescript(Object examdescript) {
        return findByProperty(EXAMDESCRIPT, examdescript);
    }

    public List<Examen> findByExamestadon(Object examestadon) {
        return findByProperty(EXAMESTADON, examestadon);
    }

    public List<Examen> findByExamidn(Object examidn) {
        return findByProperty(EXAMIDN, examidn);
    }

    public List<Examen> findByExamnombrev(Object examnombrev) {
        return findByProperty(EXAMNOMBREV, examnombrev);
    }

    public List<Examen> findByExamordenn(Object examordenn) {
        return findByProperty(EXAMORDENN, examordenn);
    }

    /**
    * Find all Examen entities.
    *
    * @return List<Examen> all Examen instances
    */
    public List<Examen> findAll() {
        log.debug("finding all Examen instances");

        try {
            String queryString = "from Examen";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Examen> findByCriteria(String whereCondition) {
        log.debug("finding Examen " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Examen model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Examen> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Examen failed", re);
            throw re;
        }
    }

    public List<Examen> findPageExamen(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Examen findPageExamen");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Examen model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Examen model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberExamen() {
        log.debug("finding Examen count");

        try {
            String queryString = "select count(*) from Examen model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
			throw re;
		}
	}

	public List<Examen> findByTipoexamidn(Object tipoExamen) {

		try {
			String queryString = "from Examen where tipoexamidn = ? order by examidn asc";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, tipoExamen);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find Dptos failed", re);
			throw re;
		}

	}

}
